package com.whfc.common.third.cvi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.third.cvi.enums.CarCameraRestCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * @ClasssName
 * @Description 车载摄像头接口调用工具类
 * @Author hw
 * @Date 2020/9/3 19:39
 * @Version 1.0
 */
public class CarCameraUtil {
    private static final Logger logger = LoggerFactory.getLogger(CarCameraUtil.class);

    private static final RestTemplate restTemplate = new RestTemplate();

    /**
     * 用户名
     */
    private static final String USERNAME = "fcwl";
    /**
     * 密码
     */
    private static final String PASSWORD = "123456";

    /**
     * 认证key
     */
    private static String key;

    /**
     * 接口IP地址
     */
    private static final String SERVICE_NAME = "http://221.233.14.5:12056";

    /**
     * 登录地址
     */
    private static final String LOGIN_PATH = "/api/v1/basic/key";
    /**
     * 获取硬件列表
     */
    private static final String DEVICES_PATH = "/api/v1/basic/devices";
    /**
     * 获取视频地址
     */
    private static final String VIDEO_PATH = "/api/v1/basic/live/video";

    /**
     * 获取视频端口号地址
     */
    private static final String PORT_PATH = "/api/v1/basic/live/port";
    /**
     * 获取视频端口号地址
     */
    private static final String  IDEO_CALENDAR = "/api/v1/basic/record/calendar";
    /**
     * 获取历史视频录像地址
     */
    private static final String VIDEO_RECORD_PATH = "/api/v1/basic/record/video";



    private static void login() {
        if (key == null) {
            String url = SERVICE_NAME + LOGIN_PATH + "?username=" + USERNAME + "&password=" + PASSWORD;
            logger.info("开始获取车载摄像头KEY,请求地址：{}", url);
            JSONObject restJson = restTemplate.getForObject(url, JSONObject.class);
            logger.info("获取车载摄像头KEY,返回结果：{}", restJson);
            Integer errorcode = restJson.getInteger("errorcode");
            if (CarCameraRestCode.SUCCESS.getValue().equals(errorcode)) {
                key = restJson.getJSONObject("data").getString("key");
                return;
            }
        }
    }

    public static JSONObject getDevice(String deviceCode) {
        login();
        String url = SERVICE_NAME + DEVICES_PATH + "?key=" + key;
        logger.info("开始获取车载摄像头设备信息,请求地址：{}", url);
        JSONObject restJson = restTemplate.getForObject(url, JSONObject.class);
        logger.info("获取车载摄像头设备信息,返回结果：{}", restJson);
        Integer errorcode = restJson.getInteger("errorcode");
        if (CarCameraRestCode.SUCCESS.getValue().equals(errorcode)) {
            for (Object obj : restJson.getJSONArray("data")) {
                JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(obj));
                if (json.getString("terid").equals(deviceCode)) {
                    return json;
                }
            }
            logger.info("没有找到{}对应的车载摄像头设备信息", deviceCode);
            return null;
        }
        return null;
    }

    public static Integer getPort() {
        login();
        String url = SERVICE_NAME + PORT_PATH + "?key=" + key;
        logger.info("开始获取车载摄像头视频端口,请求地址：{}", url);
        JSONObject restJson = restTemplate.getForObject(url, JSONObject.class);
        logger.info("获取车载摄像头视频端口,返回结果：{}", restJson);
        Integer errorcode = restJson.getInteger("errorcode");
        if (CarCameraRestCode.SUCCESS.getValue().equals(errorcode)) {
            return restJson.getJSONArray("data").getJSONObject(0).getInteger("port");
        }
        logger.info("没有获取到视频端口");
        return null;
    }

    public static List<String> getVideoUrl(String deviceCode) {
        JSONObject deviceJson = getDevice(deviceCode);
        if (deviceJson == null) {
            return null;
        }
        Integer port = getPort();
        if(port == null){
            return null;
        }
        String url = SERVICE_NAME + VIDEO_PATH + "?key=" + key + "&terid=" + deviceCode + "&audio=1&st=0&port="+port;
        List<String> list = new ArrayList<>(deviceJson.getInteger("channelcount"));
        for (int i = 1; i <= deviceJson.getInteger("channelcount"); i++) {
            String videoUrl= url+"&chl="+i;
            logger.info("开始获取车载摄像头视频地址,请求地址：{}", url);
            JSONObject restJson = restTemplate.getForObject(videoUrl, JSONObject.class);
            logger.info("获取车载摄像头视频地址,返回结果：{}", restJson);
            Integer errorcode = restJson.getInteger("errorcode");
            if (CarCameraRestCode.SUCCESS.getValue().equals(errorcode)) {
                list.add(restJson.getJSONObject("data").getString("url"));
            }
        }
        return list;
    }



    /**
     * 获取历史视频月历信息接口
     *
     * @Param terid 终端编号
     * @Param start 开始时间
     * @Param chl 通道编号id
     * */
    public static List<String> getVideoCalendar(String terid,String starttime) {
        login();
        String videoCalendarUrl = SERVICE_NAME + IDEO_CALENDAR +"?key=" + key + "&terid=" + terid
                +"&starttime="+starttime+"&st=1";
        logger.info("开始获取车载摄像头历史视频月历信息获取车载摄像头历史视频月历信息,请求地址：{}", videoCalendarUrl);
        JSONObject restJson = restTemplate.getForObject(videoCalendarUrl, JSONObject.class);
        logger.info("获取车载摄像头历史视频月历信息,请求结果：{}", restJson);
        Integer errorcode = restJson.getInteger("errorcode");
        //接受视频月历信息集合
        List list = new ArrayList<String>();
        if (CarCameraRestCode.SUCCESS.getValue().equals(errorcode)) {
            JSONArray data = restJson.getJSONArray("data");
            for(int i = 0;i < data.size();i++){
                HashMap<String,String> item =  (HashMap<String,String>)data.get(i);
                list.add(item.get("date"));
            }
            Collections.sort(list); //排序
            logger.info("获取车载摄像头历史视频月历集合,月历集合：{}", list);
            return list;
        }
        logger.info("获取车载摄像头历史视频月历集合失败");
        return null;

    }


    /**
     * 获取历史视频播放链接接口
     *
     * @Param terid 终端编号
     * @Param start 开始时间
     * @Param end 结束时间
     * @Param chl 通道编号id
     * */
    public static String getVideoRecord(String terid,String start,String end,Integer chl) {
        login();
        String videoRecordUrl = SERVICE_NAME + VIDEO_RECORD_PATH +"?key=" + key + "&terid=" + terid
                    +"&starttime="+start+"&endtime="+end + "&chl="+chl;
        logger.info("开始获取车载摄像头视频回放播放地址,请求地址：{}", videoRecordUrl);
        JSONObject restJson = restTemplate.getForObject(videoRecordUrl, JSONObject.class);
        logger.info("获取车载摄像头视频回放播放地址请求结果,请求结果：{}", restJson);
        Integer errorcode = restJson.getInteger("errorcode");
        if (CarCameraRestCode.SUCCESS.getValue().equals(errorcode)) {
            String url = restJson.getJSONObject("data").getString("url");
            logger.info("获取车载摄像头视频回放播放地址,播放地址：{}", url);
            return url;
        }
        logger.info("获取车载摄像头视频回放播放地址失败");
        return null;

    }

    public static void main(String[] args) {
       // getVideoRecord("00A60003C3","2020-12-20","2020-12-20",1);
       // System.out.println(getVideoUrl("00A60003C3").toString());
        getVideoCalendar("00A60003C3","2020-12-20");
    }

}
